package com.sinodata.bsm.inmp.bean.gab;

import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.Map;

import com.sinodata.bsm.common.vo.Res;
import com.sinodata.bsm.common.vo.ResPropertyValue;
import com.sinodata.bsm.inmp.cache.CacheFactory;
import com.sinodata.bsm.inmp.cache.CollectFieldCache;
import com.sinodata.bsm.inmp.cache.PropertyCache;
import com.sinodata.bsm.inmp.task.gab.WsdPerformaceDataCollectorJob;

/**
 * <p>
 * Description: 这个类以及其子类中的方法，final修饰的属性值和公安部温湿度的ycp表和yxp表严格保持一致，
 *              如果这两张表中的数据有改动特别是数据的增删的时候，这些类要做相应调整，
 *              否则温湿度的性能数据就会得出错误的结果
 * </p>
 *
 * @author wangpeng
 * @version 1.0

 * <p>
 * History: 
 *
 * Date                     Author         Version     Description
 * ---------------------------------------------------------------------------------
 * 2013-9-5 上午10:14:11          wangpeng        1.0         To create
 * </p>
 *
 * @since 
 * @see     
 */
public abstract class WenshiduParentBean {

    protected PropertyCache propertyCache = (PropertyCache) CacheFactory.getCache(PropertyCache.class);

    protected CollectFieldCache fieldCache = (CollectFieldCache) CacheFactory.getCache(CollectFieldCache.class);

    /**
     * 继承WenshiduParentBean的都是温湿度中涉及到的设备，都有设备号
     */
    protected int deviceNumber;

    /**
     * 继承WenshiduParentBean的类都是通过一个温湿度资源来构造
     */
    protected Res res;

    /**
     * propValueMap用于存放指标id和指标值的键值对
     */
    protected final Map<Long, Integer> propValueMap = new HashMap<Long, Integer>();

    protected static final SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    /**
     * kongtYCPropertyQuntity 表示每个空调设备在遥测表(ycp)中有多少个数据项
     * kongtYCPropertyQuntity_也表示空调设备在遥测表(ycp)中有多少个数据项(空调有两种情况)
     * upsYCPropertyQuntity 表示每个UPS设备在遥测表(ycp)中有多少个数据项
     * dianlyYCPropertyQuntity 表示每个电量仪设备在遥测表(ycp)中有多少个数据项
     * wensdYCPropertyQuntity 表示每个温湿度设备在遥测表(ycp)中有多少个数据项
     */
    private final int kongtYCPropertyQuntity = 15;
    private final int kongtYCPropertyQuntity_ = 11;
    private final int upsYCPropertyQuntity = 31;
    private final int dianlyYCPropertyQuntity = 32;
    private final int wensdYCPropertyQuntity = 2;

    private final int upsYXPropertyQuntity = 105;

    private int getYcTableStartIndexes() {
        int index = 0;
        if (deviceNumber <= 8) {
            index = (deviceNumber - 1) * kongtYCPropertyQuntity;
        } else if (deviceNumber <= 13) {
            index = 120 + (deviceNumber - 8 - 1) * wensdYCPropertyQuntity;
        } else if (deviceNumber <= 14) {
            index = 130;
        } else if (deviceNumber <= 20) {
            index = 147 + (deviceNumber - 14 - 1) * dianlyYCPropertyQuntity;
        } else if (deviceNumber <= 27) {
            index = 339 + (deviceNumber - 20 - 1) * upsYCPropertyQuntity;
        } else if (deviceNumber <= 39) {
            index = 556 + (deviceNumber - 29 - 1) * kongtYCPropertyQuntity_;
        } else {
            index = 666 + (deviceNumber - 39 - 1) * wensdYCPropertyQuntity;
        }
        return index;
    }

    private int getYxTableIndex(int index) {
        return (deviceNumber - 21) * upsYXPropertyQuntity + 483 + index;
    }

    private int getYcTableIndex(int index) {
        return getYcTableStartIndexes() + index;
    }

    protected final Integer getYcValueFromDataMap(int index) {
        return WsdPerformaceDataCollectorJob.getYCValueByIndex(getYcTableIndex(index));
    }

    protected final Integer getYXValueFromDataMap(int index) {
        return WsdPerformaceDataCollectorJob.getYXValueByIndex(getYxTableIndex(index));
    }

    public abstract ResPropertyValue getPropertyValue();
}
